package co.fitstart.admin.entity.search;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

import co.fitstart.entity.course.CourseType;
import co.fitstart.entity.site.Site;

/**
 * 
 * @author lidan
 *
 */
public class OfflineStudentSearch implements QueryBuilder {
    
    private String name;
    
    private String mobile;
    
    private Long site;
    
    private List<Site> sites;
    
    private Order order;
    
    private Filter filter;
    
    public OfflineStudentSearch() {
       order = new Order("round.student.createTime", "DESC");
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {
        
        
        
        if(filter == null || refresh) {
            Map<String, Object> parameters = new HashMap<String, Object>();
            parameters.put("type", new CourseType[]{CourseType.INDIVIDUAL, CourseType.SPECIAL, CourseType.POSTURE, CourseType.YEAR});
            
            filter = new Filter().addLike("round.student.name", name)
                                 .addLike("round.student.mobile", mobile)
                                 .addEquals("round.site.id", site)
                                 .addSentence("round.site IN (:sites)", "sites", sites)
                                 .addSentence("round.courseType in (:type)", parameters)
                                 .addSentence("round.student.stat.individualCount+round.student.stat.groupCount+round.student.stat.yearCount > 0", new HashMap<String, Object>());
            
        }
        
        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        String sentence = "SELECT round.student FROM CourseRound round WHERE round.disabled = false AND round.student.disabled = false " + getPreparedFilter(false).getClauseSentence() + " GROUP BY round.student" + order.getSentence();
        return sentence;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the mobile
     */
    public String getMobile() {
        return mobile;
    }

    /**
     * @return the site
     */
    public Long getSite() {
        return site;
    }

    /**
     * @return the sites
     */
    public List<Site> getSites() {
        return sites;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    /**
     * @param site the site to set
     */
    public void setSite(Long site) {
        this.site = site;
    }

    /**
     * @param sites the sites to set
     */
    public void setSites(List<Site> sites) {
        this.sites = sites;
    }
}
